package com.dl.config;

import com.dl.interceptor.RememberMeInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author DL
 * @create 2021-01-26 10:52
 * 扩展mvc配置
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    //拦截器
    @Resource
    private RememberMeInterceptor rememberMeInterceptor;

/*    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }*/

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //静态资源路径
        String[] resourcesPath = {"/static/**","/upload/**","/avatar/**"};
        //公共请求路径
        String[] commonsRequest = {"/","/login","/captcha","/check","/register"};

        registry.addInterceptor(rememberMeInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(resourcesPath)
                .excludePathPatterns("/swagger-ui.html")
                .excludePathPatterns(commonsRequest);

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        String os = System.getProperty("os.name");//获取系统环境

        String avatarPathWindow = System.getProperty("user.dir") + "\\avatar\\";
        String uploadPathWindow = System.getProperty("user.dir") + "\\upload\\";

        String avatarPathLinux = "/user/huixuexi/avatar/";
        String uploadPathLinux = "/user/huixuexi/avatar/";

        //如果是Windows系统
        if (os.toLowerCase().startsWith("win")) {
            //表示文件路径，这里的意思是picture包下的所有文件，所有/picture/开头的请求 都会去后面配置的路径下查找资源
            registry.addResourceHandler("/avatar/**")
                    .addResourceLocations("file:" + avatarPathWindow);
            registry.addResourceHandler("/upload/**")
                    .addResourceLocations("file:" + uploadPathWindow);
        } else {  //linux 和mac
            registry.addResourceHandler("/avatar/**")
                    .addResourceLocations("file:" + avatarPathLinux);
            registry.addResourceHandler("/upload/**")
                    .addResourceLocations("file:" + uploadPathLinux);
        }
    }
}
